package com.course.controller;

import com.course.config.CodeGenerator;
import com.course.entity.ResultBody;
import com.course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/course")
@CrossOrigin(origins = "*")
public class CourseController {

    @Autowired
    private CourseService courseService;
    @RequestMapping("/addcourse")
    //增加课程
    public ResultBody addcourse(@RequestBody Map<String, Object> map) {
        //判断参数是否完整
        if (map.get("coursename") == null || map.get("coursetype") == null || map.get("coursecollege") == null || map.get("coursrdescription") == null || map.get("coursegoals") == null||map.get("coursetime")==null||map.get("courseteacher")==null) {
            return new ResultBody(404, "参数错误", false);
        }
        map.put("coursenumber",CodeGenerator.generateId());
        return courseService.addcourse(map);
    }

    @RequestMapping("/updatecourse")
    //更新课程信息
    public ResultBody updatecourse(@RequestBody Map<String, Object> map){
        return courseService.updatecourse(map);
    }

    @RequestMapping("/deletecourse")
    //删除课程课程
    public ResultBody deletecourse(@RequestBody Map<String, Object> map){
        if(map.get("courseid") == null){
            return new ResultBody(400, "课程id不能为空", null);
        }
        long courseid = Long.parseLong(map.get("courseid").toString());
        return courseService.deletecourse(courseid);
    }

}
